package com.example.wwk.graduationpractice.AsyncTask;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.example.wwk.graduationpractice.NetDataUtils.LitePalUtils;
import com.example.wwk.graduationpractice.Globalutils.CircleImageUtils;
import com.example.wwk.graduationpractice.Globalutils.FormatTools;
import com.example.wwk.graduationpractice.R;
import com.example.wwk.graduationpractice.SmackUtils.GetVcardUtils;
import com.example.wwk.graduationpractice.Bean.ComeMsgBean;
import com.example.wwk.graduationpractice.Bean.PsychologistBean;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;

import java.util.List;

/**
 * Created by wwk on 2017/3/18.
 */

public class PsychologyListAsyncTask extends AppCompatActivity {

    public static List<PsychologistBean> initPsychologists(final AbstractXMPPConnection connection,
                                                           final List<PsychologistBean> psychologists, final Roster roster, final List<RosterEntry> entries, final String receiver) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String status;
                int statusColor;
                psychologists.clear();
                PsychologistBean ps;
                String user;
                String userName;
                for (RosterEntry entry : entries) {
                    user = entry.getUser();
                    Presence presence = roster.getPresence(user);
                    //判断是否在线
                    if (presence.isAvailable()) {
                        status = "在线";
                        statusColor = 1;
                    } else {
                        status = "离线";
                        statusColor = 0;
                    }
                    //未读信息数量获得 unreadCount
                    userName = user.substring(0, user.indexOf("@"));
                    int unreadCount = 0;
                    List<ComeMsgBean> comeMsgBeen = LitePalUtils.querryData(receiver, userName);
                    if (comeMsgBeen.size() != 0) {      //当没有这两者的消息记录
                        for (ComeMsgBean comeMsgBeen1 : comeMsgBeen) {
                            unreadCount = comeMsgBeen1.getUnreadCount() ;
                            Log.d("123", "doInBackground: "+unreadCount);
                            LitePalUtils.updateData(unreadCount, receiver, userName);
                        }
                    }
                    Drawable headerImage = GetVcardUtils.getUserImage(connection, user.substring(0, user.indexOf("@")));
                    if (headerImage != null) {
                        int size = 200;
                        Bitmap scaleBitmap = Bitmap.createScaledBitmap(FormatTools.getInstance().drawable2Bitmap(headerImage), size, size, true);//压缩图片
                        ps = new PsychologistBean(userName, CircleImageUtils.createCircleImage(scaleBitmap), status, "hello", statusColor, user, unreadCount);
                    } else {
                        ps = new PsychologistBean(userName, R.drawable.default_header, status, "hello", statusColor, user, unreadCount);
                    }
                    if (status.equals("在线"))
                        psychologists.add(0, ps);
                    else
                        psychologists.add(psychologists.size(), ps);
                }
            }
        }).start();
//        new AsyncTask<String, String, Integer>() {
//
//            @Override
//            protected Integer doInBackground(String... strings) {
//
//                return 0;
//            }
//
//        }.execute();
        return psychologists;
    }

}
